Skip to content

perf : Optimize count distinct#21456

Open
coderfender wants to merge 11 commits intoapache:mainfrom
coderfender:optimize_count_distinct
Open

perf : Optimize count distinct#21456
coderfender wants to merge 11 commits intoapache:mainfrom
coderfender:optimize_count_distinct

Conversation

@coderfender
Copy link
Copy Markdown
Contributor

@coderfender coderfender commented Apr 8, 2026

Which issue does this PR close?

Remove hashset based accumulators for smaller int data types and use bitmaps. Follow up of : #21453

Rationale for this change

What changes are included in this PR?

Are these changes tested?

Are there any user-facing changes?

@github-actions github-actions bot added the functions Changes to functions implementation label Apr 8, 2026
@coderfender
Copy link
Copy Markdown
Contributor Author

coderfender commented Apr 8, 2026

benchmark results :

count_distinct i16 bitmap                      1.00      3.3±0.43µs        ? ?/sec    23.87    78.4±0.84µs        ? ?/sec
count_distinct i8 bitmap                       1.00      2.3±0.49µs        ? ?/sec    7.13     16.7±0.55µs        ? ?/sec
count_distinct u16 bitmap                      1.00      3.1±0.18µs        ? ?/sec    25.45    78.8±3.92µs        ? ?/sec
count_distinct u8 bitmap                       1.00      2.3±0.34µs        ? ?/sec    7.37     16.9±0.14µs        ? ?/sec

It seems like we are 25x faster for u16 bitmap based accumulators (or I am sleepy :) )

@Dandandan
Copy link
Copy Markdown
Contributor

I think we can do the same for 16 bit types, it is just 65_536 bytes 8192 if we use a bitmap.

@Dandandan
Copy link
Copy Markdown
Contributor

Oh wait, you're already doing that :)

@coderfender
Copy link
Copy Markdown
Contributor Author

coderfender commented Apr 8, 2026

Query 0 in clickbench_extended dataset (which uses count distinct on u8 is now ~ 11 % faster :

┃ Query     ┃    main_cb ┃ bitmap_cb_2 ┃        Change ┃
┡━━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│ QQuery 0  │  529.46 ms │   478.99 ms │ +1.11x faster │

(Other queries are faster but I believe that is more around variance )

┏━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
┃ Query     ┃    main_cb ┃ bitmap_cb_2 ┃        Change ┃
┡━━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│ QQuery 0  │  529.46 ms │   478.99 ms │ +1.11x faster │
│ QQuery 1  │  107.43 ms │   102.59 ms │     no change │
│ QQuery 2  │  250.89 ms │   240.76 ms │     no change │
│ QQuery 3  │  207.67 ms │   207.49 ms │     no change │
│ QQuery 4  │  391.43 ms │   353.05 ms │ +1.11x faster │
│ QQuery 5  │ 4144.11 ms │  4084.08 ms │     no change │
│ QQuery 6  │  676.03 ms │   622.21 ms │ +1.09x faster │
│ QQuery 7  │  719.78 ms │   599.06 ms │ +1.20x faster │
│ QQuery 8  │  238.30 ms │   207.27 ms │ +1.15x faster │
│ QQuery 9  │ 1531.52 ms │  1406.34 ms │ +1.09x faster │
│ QQuery 10 │  435.27 ms │   403.28 ms │ +1.08x faster │
│ QQuery 11 │ 1043.68 ms │   955.22 ms │ +1.09x faster │
│ QQuery 12 │  113.16 ms │   106.31 ms │ +1.06x faster │
└───────────┴────────────┴─────────────┴───────────────┘
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┓
┃ Benchmark Summary          ┃            ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━┩
│ Total Time (main_cb)       │ 10388.73ms │
│ Total Time (bitmap_cb_2)   │  9766.66ms │
│ Average Time (main_cb)     │   799.13ms │
│ Average Time (bitmap_cb_2) │   751.28ms │
│ Queries Faster             │          9 │
│ Queries Slower             │          0 │
│ Queries with No Change     │          4 │
│ Queries with Failure       │          0 │
└────────────────────────────┴────────────┘

@coderfender
Copy link
Copy Markdown
Contributor Author

cc : @neilconway , @alamb , @martin-g . Please take a look whenever you get a chance

@alamb

This comment has been minimized.

@alamb alamb added the performance Make DataFusion faster label Apr 9, 2026
Copy link
Copy Markdown
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a great idea. Thank you @coderfender

harness = false

[[bench]]
name = "count_distinct"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please add this benchmark as a separate PR (so we can use our standard benchmark runner to confirm the results)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats sounds like a good idea . Let me put up a PR to add benchmarks to count_distinct , approx_distinct on a separate PR

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Raised : #21521

@adriangbot

This comment has been minimized.

@adriangbot

This comment has been minimized.

@adriangbot

This comment has been minimized.

/// Uses 256 bytes to track all possible u8 values.
#[derive(Debug)]
pub struct BoolArray256DistinctCountAccumulator {
seen: Box<[bool; 256]>,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can probably use a BooleanBuffer from Arrow to make this signifcantly faster (I think [bool uses a byte for each booelan) 🤔

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea ! Let me try and experiment with that

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For small arrays this might not be necessarily faster (as it fits in L1 cache) - perhaps only if you can use instructions like popcount etc.?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although it probably wouldn't be very different anyway.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like BooleanBuffer is immutable. I didnt have luck using [u8:8] while trying to optimize approx_distinct :
#21453 (comment) and resorted the approach which yielded good speed up (~2x) vs regression using[u8;8]

Copy link
Copy Markdown
Contributor Author

@coderfender coderfender Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Results using MutableBuffer : (slower than directly using bitmap)

group                                          bitmap_count_distinct                  bitmap_count_distinct_mutable_buffer    main
-----                                          ---------------------                  ------------------------------------    ----    6.2±0.22µs        ? ?/sec
count_distinct i16 bitmap                      1.03      3.1±0.14µs        ? ?/sec    1.00      3.0±0.02µs        ? ?/sec     26.16    77.9±3.43µs        ? ?/sec
count_distinct i64 80% distinct                1.03     48.2±0.44µs        ? ?/sec    1.00     47.0±0.40µs        ? ?/sec     1.02     48.0±1.49µs        ? ?/sec
count_distinct i64 99% distinct                1.02     48.3±0.46µs        ? ?/sec    1.00     47.2±0.92µs        ? ?/sec     1.02     48.3±2.14µs        ? ?/sec
count_distinct i8 bitmap                       1.00      2.9±0.43µs        ? ?/sec    1.37      3.9±0.06µs        ? ?/sec     5.75     16.4±0.26µs        ? ?/sec
count_distinct u16 bitmap                      1.06      3.2±0.30µs        ? ?/sec    1.00      3.0±0.16µs        ? ?/sec     25.58    77.7±1.82µs        ? ?/sec
count_distinct u8 bitmap                       1.00      2.1±0.03µs        ? ?/sec    1.90      3.9±0.08µs        ? ?/sec     8.14  

@adriangbot

This comment has been minimized.

@adriangbot

This comment has been minimized.

@alamb
Copy link
Copy Markdown
Contributor

alamb commented Apr 9, 2026

run benchmark count_distinct

@alamb
Copy link
Copy Markdown
Contributor

alamb commented Apr 9, 2026

run benchmark clickbench

@adriangbot
Copy link
Copy Markdown

🤖 Criterion benchmark running (GKE) | trigger
Instance: c4a-highmem-16 (12 vCPU / 65 GiB) | Linux bench-c4217267905-1036-wmjln 6.12.55+ #1 SMP Sun Feb 1 08:59:41 UTC 2026 aarch64 GNU/Linux

CPU Details (lscpu)
Architecture:                            aarch64
CPU op-mode(s):                          64-bit
Byte Order:                              Little Endian
CPU(s):                                  16
On-line CPU(s) list:                     0-15
Vendor ID:                               ARM
Model name:                              Neoverse-V2
Model:                                   1
Thread(s) per core:                      1
Core(s) per cluster:                     16
Socket(s):                               -
Cluster(s):                              1
Stepping:                                r0p1
BogoMIPS:                                2000.00
Flags:                                   fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 sm3 sm4 asimddp sha512 sve asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp sve2 sveaes svepmull svebitperm svesha3 svesm4 flagm2 frint svei8mm svebf16 i8mm bf16 dgh rng bti
L1d cache:                               1 MiB (16 instances)
L1i cache:                               1 MiB (16 instances)
L2 cache:                                32 MiB (16 instances)
L3 cache:                                80 MiB (1 instance)
NUMA node(s):                            1
NUMA node0 CPU(s):                       0-15
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Not affected
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; CSV2, BHB
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Not affected

Comparing optimize_count_distinct (a13bcaa) to fbdf770 (merge-base) diff
BENCH_NAME=count_distinct
BENCH_COMMAND=cargo bench --features=parquet --bench count_distinct
BENCH_FILTER=
Results will be posted here when complete


File an issue against this benchmark runner

@adriangbot
Copy link
Copy Markdown

Benchmark for this request failed.

Last 20 lines of output:

Click to expand
rustc 1.94.1 (e408947bf 2026-03-25)
a13bcaad5b82e69257e82b741cf72619da838990
fbdf7703a96408b4eba27801431be8bf468734d8
error: failed to load manifest for workspace member `/workspace/datafusion-branch/datafusion/catalog`
referenced by workspace at `/workspace/datafusion-branch/Cargo.toml`

Caused by:
  failed to load manifest for dependency `datafusion-datasource`

Caused by:
  failed to load manifest for dependency `datafusion-physical-plan`

Caused by:
  failed to load manifest for dependency `datafusion-functions-aggregate`

Caused by:
  failed to parse manifest at `/workspace/datafusion-branch/datafusion/functions-aggregate/Cargo.toml`

Caused by:
  found duplicate bench name count_distinct, but all bench targets must have a unique name

File an issue against this benchmark runner

@adriangbot
Copy link
Copy Markdown

🤖 Criterion benchmark running (GKE) | trigger
Instance: c4a-highmem-16 (12 vCPU / 65 GiB) | Linux bench-c4217270691-1037-8wdsm 6.12.55+ #1 SMP Sun Feb 1 08:59:41 UTC 2026 aarch64 GNU/Linux

CPU Details (lscpu)
Architecture:                            aarch64
CPU op-mode(s):                          64-bit
Byte Order:                              Little Endian
CPU(s):                                  16
On-line CPU(s) list:                     0-15
Vendor ID:                               ARM
Model name:                              Neoverse-V2
Model:                                   1
Thread(s) per core:                      1
Core(s) per cluster:                     16
Socket(s):                               -
Cluster(s):                              1
Stepping:                                r0p1
BogoMIPS:                                2000.00
Flags:                                   fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 sm3 sm4 asimddp sha512 sve asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp sve2 sveaes svepmull svebitperm svesha3 svesm4 flagm2 frint svei8mm svebf16 i8mm bf16 dgh rng bti
L1d cache:                               1 MiB (16 instances)
L1i cache:                               1 MiB (16 instances)
L2 cache:                                32 MiB (16 instances)
L3 cache:                                80 MiB (1 instance)
NUMA node(s):                            1
NUMA node0 CPU(s):                       0-15
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Not affected
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; CSV2, BHB
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Not affected

Comparing optimize_count_distinct (a13bcaa) to fbdf770 (merge-base) diff
BENCH_NAME=clickbench
BENCH_COMMAND=cargo bench --features=parquet --bench clickbench
BENCH_FILTER=
Results will be posted here when complete


File an issue against this benchmark runner

@adriangbot
Copy link
Copy Markdown

Benchmark for this request failed.

Last 20 lines of output:

Click to expand
rustc 1.94.1 (e408947bf 2026-03-25)
a13bcaad5b82e69257e82b741cf72619da838990
fbdf7703a96408b4eba27801431be8bf468734d8
error: failed to load manifest for workspace member `/workspace/datafusion-branch/datafusion/catalog`
referenced by workspace at `/workspace/datafusion-branch/Cargo.toml`

Caused by:
  failed to load manifest for dependency `datafusion-datasource`

Caused by:
  failed to load manifest for dependency `datafusion-physical-plan`

Caused by:
  failed to load manifest for dependency `datafusion-functions-aggregate`

Caused by:
  failed to parse manifest at `/workspace/datafusion-branch/datafusion/functions-aggregate/Cargo.toml`

Caused by:
  found duplicate bench name count_distinct, but all bench targets must have a unique name

File an issue against this benchmark runner

@coderfender coderfender force-pushed the optimize_count_distinct branch from a13bcaa to f8c01a1 Compare April 9, 2026 21:33
@coderfender
Copy link
Copy Markdown
Contributor Author

Rebased with main

@coderfender coderfender force-pushed the optimize_count_distinct branch from 3db92e3 to df90ef5 Compare April 9, 2026 21:38
@coderfender
Copy link
Copy Markdown
Contributor Author

Update benchmarks after rebase with main

Command :

cargo bench -p datafusion-functions-aggregate --bench count_distinct
group                                          bitmap_count_distinct                  main
-----                                          ---------------------                  ----
count_distinct i16 bitmap                      1.00      3.1±0.14µs        ? ?/sec    25.35    77.9±3.43µs        ? ?/sec
count_distinct i64 80% distinct                1.00     48.2±0.44µs        ? ?/sec    1.00     48.0±1.49µs        ? ?/sec
count_distinct i64 99% distinct                1.00     48.3±0.46µs        ? ?/sec    1.00     48.3±2.14µs        ? ?/sec
count_distinct i8 bitmap                       1.00      2.9±0.43µs        ? ?/sec    5.75     16.4±0.26µs        ? ?/sec
count_distinct u16 bitmap                      1.00      3.2±0.30µs        ? ?/sec    24.08    77.7±1.82µs        ? ?/sec
count_distinct u8 bitmap                       1.00      2.1±0.03µs        ? ?/sec    8.14     16.8±0.05µs        ? ?/sec

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

functions Changes to functions implementation performance Make DataFusion faster

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use bitmap for count_distinct expression for u8/16 and i8/16 [perf]

4 participants